package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.AnnouncementService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.AnnouncementVo;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private QuestionService questionService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;


    //打招呼
    public void strangerQuestions(Long jiarenId, String reply) {
        Long userId = UserHolder.get().getId();
        String nickname = userInfoService.findById(userId).getNickname();
        Question question = questionService.findByUserId(jiarenId);
        if (question == null) {
            question = new Question();
        }
        //组装消息
        Map requestBody = new HashMap();
        requestBody.put("userId", userId);
        requestBody.put("huanXinId", "HX" + userId);
        requestBody.put("nickname", nickname);
        requestBody.put("strangerQuestion", question.getStrangerQuestion());
        requestBody.put("reply", reply);
        String json = JSON.toJSONString(requestBody);
        huanXinTemplate.sendMsg("HX" + jiarenId, json);

    }

    @DubboReference
    private FriendService friendService;
    //加好友
    public void addContacts(Long friendId) {
        Long userId = UserHolder.get().getId();

        friendService.addContacts(userId, friendId);

        huanXinTemplate.addContacts("HX" + userId, "HX" + friendId);
    }

    public ResponseEntity findContactVoByPage(Integer pageNum, Integer pageSize) {
        Long userId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = friendService.findContactByPage(userId, pageNum, pageSize);
        List<ContactVo> voList = new ArrayList<>();
        List<Friend> friends = (List<Friend>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(friends)) {
            for (Friend friend : friends) {
                UserInfo userInfo = userInfoService.findById(friend.getFriendId());
                ContactVo vo = new ContactVo();
                vo.setUserInfo(userInfo);
                vo.setUserId("HX" + friend.getFriendId());
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);
    }

    @DubboReference
    private CommentService commentService;
    //查看点赞 喜欢 评论
    public ResponseEntity findUserCommentVoByPage(Integer commentType, Integer pageNum, Integer pageSize) {

        Long userId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = commentService.findUserCommentByPage(userId, commentType, pageNum, pageSize);
        List<UserCommentVo> voList = new ArrayList<>();
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                UserCommentVo vo = new UserCommentVo();
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                vo.setId(comment.getId().toHexString());
                vo.setNickname(userInfo.getNickname());
                vo.setAvatar(userInfo.getAvatar());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);
    }

    @DubboReference
    private AnnouncementService announcementService;

    //公告
    public ResponseEntity announcementsByPage(Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = announcementService.announcementByPage(pageNum, pageSize);

        List<AnnouncementVo> voList = new ArrayList<>();
        List<Announcement> announcements = (List<Announcement>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(announcements)) {
            for (Announcement announcement : announcements) {
                AnnouncementVo vo = new AnnouncementVo();
                vo.setId(String.valueOf(announcement.getId()));
                vo.setTitle(announcement.getTitle());
                vo.setDescription(announcement.getDescription());
                vo.setCreateDate(DateUtil.formatDateTime(announcement.getCreated()));
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);
    }
}
